package com.pool;

import java.io.IOException;

import org.apache.commons.pool.BasePoolableObjectFactory;

public class MyResourceFactory extends BasePoolableObjectFactory<Resource>{

	@Override
	public Resource makeObject() throws Exception {
		Resource resource = new Resource(22, "127.0.0.1");
		resource.connect();
		if(1 == 1 ) {
			
			throw new IllegalStateException();
		}
		return resource;
	}

	@Override
	public void destroyObject(Resource resource) throws Exception {
		resource.close();
	}
	
	@Override
	public boolean validateObject(Resource resource) {
		return resource.isConnect();
	}
	
	/**
	 * activateObject 和 passivateObject 相对， 激活和钝化， 钝化并不是将对象放逐，而是归还active时的资源。
	 */
	@Override
	public void activateObject(Resource resource) throws Exception {
		System.out.println("active resource");
		super.activateObject(resource);
	}
	
	@Override
	public void passivateObject(Resource resource) throws Exception {
		System.out.println("passivate resource");
		super.passivateObject(resource);
	}
}
